package com.etb.wisdom.eframe.framework.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.etb.core.pojo.CriteriaType;
import com.etb.core.service.ETBService;
import com.etb.web.eui.tree.TreeNode;
import com.etb.web.eui.tree.TreeNode.State;
import com.etb.wisdom.eframe.framework.service.AppApplicationService;
import com.etb.wisdom.eframe.model.AppApplication;
import com.etb.wisdom.eframe.model.AppFuncgroup;
import com.etb.wisdom.eframe.model.AppFunction;
import com.etb.wisdom.eframe.model.EtbPartyRes;

@Service
public class AppApplicationServiceImpl extends ETBService implements AppApplicationService {

	@Override
	public List<TreeNode> getTreenode(String id, String nodeType) {
		List<TreeNode> treedata = new ArrayList<TreeNode>();
		if(StringUtils.isBlank(id)) {
			TreeNode treenode = new TreeNode(TREENODEID_ROOT, TREENODENAME_ROOT);
			treenode.put("nodeType", TREENODETYPE_ROOT);
			treenode.setState(State.closed.toString());
			treenode.setIconCls("icon-root");
			treedata.add(treenode);
		} else if(id.equals(TREENODEID_ROOT)) {
			List<AppApplication> apps = service.queryAll(AppApplication.class);
			for(AppApplication app: apps) {
				TreeNode treenode = new TreeNode(app.getId(), app.getAppname());
				treenode.put("nodeType", TREENODETYPE_APPLICATION);
				treenode.setState(State.closed.toString());
				treenode.setIconCls("icon-app");
				treedata.add(treenode);
			}
		} else if(TREENODETYPE_APPLICATION.equals(nodeType)) {
			AppFuncgroup groupTemplate = new AppFuncgroup();
			groupTemplate.setApplicationid(id);
			groupTemplate.setGrouplevel("1");
			List<AppFuncgroup> groups = service.query(groupTemplate);
			for(AppFuncgroup group: groups) {
				TreeNode treenode = new TreeNode(group.getId(), group.getGroupname());
				treenode.put("nodeType", TREENODETYPE_FUNCGROUP);
				treenode.put("groupLevel", group.getGrouplevel());
				treenode.setState(State.closed.toString());
				treedata.add(treenode);
			}
		} else if(TREENODETYPE_FUNCGROUP.equals(nodeType)) {
			AppFuncgroup groupTemplate = new AppFuncgroup();
			groupTemplate.setUplevelid(id);
			List<AppFuncgroup> groups = service.query(groupTemplate);
			for(AppFuncgroup group: groups) {
				TreeNode treenode = new TreeNode(group.getId(), group.getGroupname());
				treenode.put("nodeType", TREENODETYPE_FUNCGROUP);
				treenode.put("groupLevel", group.getGrouplevel());
				treenode.setState(State.closed.toString());
				treedata.add(treenode);
			}
			AppFunction funcTemplate = new AppFunction();
			funcTemplate.setGroupid(id);
			List<AppFunction> funcs = service.query(funcTemplate);
			for(AppFunction func: funcs) {
				TreeNode treenode = new TreeNode(func.getId(), func.getFuncname());
				treenode.put("nodeType", TREENODETYPE_FUNCTION);
				treenode.setIconCls("icon-func");
				treedata.add(treenode);
			}
		}
		return treedata;
	}
	
	@Override
	public List<TreeNode> getFuncTreenode(String id, String nodeType, String value) {
		List<TreeNode> treedata = new ArrayList<TreeNode>();
		if(StringUtils.isBlank(id)) {
			if(StringUtils.isNotBlank(value)) {
				AppFunction func = new AppFunction();
				func.setId(value);
				service.extend(func);
				TreeNode treenode = new TreeNode(func.getId(), func.getFuncname());
				treenode.put("nodeType", TREENODETYPE_FUNCTION);
				treenode.setIconCls("icon-func");
				treedata.add(treenode);
			}
			TreeNode treenode = new TreeNode(TREENODEID_ROOT, TREENODENAME_ROOT);
			treenode.put("nodeType", TREENODETYPE_ROOT);
			treenode.setState(State.closed.toString());
			treenode.setIconCls("icon-root");
			treedata.add(treenode);
		} else if(id.equals(TREENODEID_ROOT)) {
			List<AppApplication> apps = service.queryAll(AppApplication.class);
			for(AppApplication app: apps) {
				TreeNode treenode = new TreeNode(app.getId(), app.getAppname());
				treenode.put("nodeType", TREENODETYPE_APPLICATION);
				treenode.setState(State.closed.toString());
				treenode.setIconCls("icon-app");
				treedata.add(treenode);
			}
		} else if(TREENODETYPE_APPLICATION.equals(nodeType)) {
			AppFuncgroup groupTemplate = new AppFuncgroup();
			groupTemplate.setApplicationid(id);
			groupTemplate.setGrouplevel("1");
			List<AppFuncgroup> groups = service.query(groupTemplate);
			for(AppFuncgroup group: groups) {
				TreeNode treenode = new TreeNode(group.getId(), group.getGroupname());
				treenode.put("nodeType", TREENODETYPE_FUNCGROUP);
				treenode.put("groupLevel", group.getGrouplevel());
				treenode.setState(State.closed.toString());
				treedata.add(treenode);
			}
		} else if(TREENODETYPE_FUNCGROUP.equals(nodeType)) {
			AppFuncgroup groupTemplate = new AppFuncgroup();
			groupTemplate.setUplevelid(id);
			List<AppFuncgroup> groups = service.query(groupTemplate);
			for(AppFuncgroup group: groups) {
				TreeNode treenode = new TreeNode(group.getId(), group.getGroupname());
				treenode.put("nodeType", TREENODETYPE_FUNCGROUP);
				treenode.put("groupLevel", group.getGrouplevel());
				treenode.setState(State.closed.toString());
				treedata.add(treenode);
			}
			AppFunction funcTemplate = new AppFunction();
			funcTemplate.setGroupid(id);
			List<AppFunction> funcs = service.query(funcTemplate);
			for(AppFunction func: funcs) {
				TreeNode treenode = new TreeNode(func.getId(), func.getFuncname());
				treenode.put("nodeType", TREENODETYPE_FUNCTION);
				treedata.add(treenode);
			}
		}
		return treedata;
	}

	@Override
	public List<TreeNode> getAuth(String roleId) {
		List<TreeNode> treedata = new ArrayList<TreeNode>();
		TreeNode treenode = new TreeNode(TREENODEID_ROOT, TREENODENAME_ROOT);
		treenode.put("nodeType", TREENODETYPE_ROOT);
		treenode.setState(State.closed.toString());
		treenode.setIconCls("icon-root");
		treenode.setState(State.open.toString());
		treedata.add(treenode);
		
		List<AppApplication> apps = service.query(new CriteriaType(AppApplication.class,"id,appname"));
		for(AppApplication app: apps) {
			treenode = new TreeNode(app.getId(), TREENODEID_ROOT, app.getAppname());
			treenode.put("nodeType", TREENODETYPE_APPLICATION);
			treenode.setIconCls("icon-app");
			treenode.setState(State.open.toString());
			treedata.add(treenode);
		}
		List<AppFuncgroup> groups = service.query(new CriteriaType(AppFuncgroup.class,"id,groupname,applicationid,uplevelid"));
		for(AppFuncgroup group: groups) {
			treenode = new TreeNode(group.getId(), group.getGroupname());
			if(StringUtils.isBlank(group.getUplevelid())) {
				treenode.setPid(group.getApplicationid());
			} else {
				treenode.setPid(group.getUplevelid());
			}
			treenode.put("nodeType", TREENODETYPE_FUNCGROUP);
			treenode.put("groupLevel", group.getGrouplevel());
			treenode.setState(State.closed.toString());
			treedata.add(treenode);
		}
		
		EtbPartyRes res = new EtbPartyRes();
		res.setPartyid(roleId);
		res.setPartytype("role");
		res.setRestype("function");
		service.querySpecifiedProperty(res, "resid");
		List<String> resList = service.querySpecifiedProperty(res, "resid");
		
		List<AppFunction> funcs = service.query(new CriteriaType(AppFunction.class,"id,funcname,groupid"));
		for(AppFunction func: funcs) {
			treenode = new TreeNode(func.getId(),func.getGroupid(), func.getFuncname());
			treenode.put("nodeType", TREENODETYPE_FUNCTION);
			treenode.setIconCls("icon-func");
			if(resList.contains(func.getId())) {
				treenode.setChecked("true");
			}
			treedata.add(treenode);
		}
		return treedata;
	}
	
}
